package com.jia.together.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jia.together.entity.Course;
import com.jia.together.entity.Member;
import com.jia.together.entity.MemberCourse;
import com.jia.together.entity.MemberCourse;
import com.jia.together.mapper.MemberCourseMapper;
import com.jia.together.service.CourseService;
import com.jia.together.service.MemberCourseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jia.together.service.MemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author dujiajia
 * @since 2021-02-24
 */
@Service
public class MemberCourseServiceImpl extends ServiceImpl<MemberCourseMapper, MemberCourse> implements MemberCourseService {

    @Autowired
    private CourseService courseService;
    @Autowired
    private MemberService memberService;

    @Override
    public Map<String, Object> selectPageList(Long pageNum, Long pageSize) {

        Page<MemberCourse> page = new Page<>(pageNum, pageSize);
        QueryWrapper<MemberCourse> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("gmt_create");
        Page<MemberCourse> resourcePage = baseMapper.selectPage(page, wrapper);
        List<MemberCourse> records = resourcePage.getRecords();
        records.forEach(item->{
            Course buyCourse = courseService.getById(item.getBuyCourseId());
            if (buyCourse!=null){
                item.setBuyCourseName(buyCourse.getTitle());
            }
            Course studyCourse = courseService.getById(item.getStudyCourseId());
            if(studyCourse != null){
                item.setStudyCourseName(studyCourse.getTitle());
            }
            Member member = memberService.getById(item.getMemberId());
            if (member !=null){
                item.setMemberName(member.getNickname());
            }
        });
        long total = resourcePage.getTotal();
        Map<String, Object> map = new HashMap<>();
        map.put("records", records);
        map.put("total", total);
        return map;

    }
}
